package com.jhf.skypac_pda.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.SimpleViewSwitcher;
import com.jcodecraeer.xrecyclerview.progressindicator.AVLoadingIndicatorView;
import com.jhf.skypac_pda.R;
import com.jhf.skypac_pda.utils.ScreenUtil;

/**
 * Created by zhoushicheng on 2018/7/12.
 */

public class CustomLoadingMoreFooter extends LinearLayout {
    private SimpleViewSwitcher progressCon;

    private TextView mText;
    private String loadingHint;
    private String noMoreHint;
    private String loadingDoneHint;
    private Context context;

    private AVLoadingIndicatorView progressView;

    public CustomLoadingMoreFooter(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public CustomLoadingMoreFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    public void initView() {
        setGravity(Gravity.CENTER);
        setLayoutParams(new RecyclerView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtil.dp2px(context,80)));
        progressCon = new SimpleViewSwitcher(getContext());
        progressCon.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ScreenUtil.dp2px(context,80)));

        progressView = new AVLoadingIndicatorView(this.getContext());
        progressView.setIndicatorColor(0xffB5B5B5);
        progressView.setIndicatorId(ProgressStyle.BallSpinFadeLoader);
        progressCon.setView(progressView);

        addView(progressCon);
        mText = new TextView(getContext());
        mText.setText(getContext().getString(R.string.listview_loading));

        if (loadingHint == null || loadingHint.equals("")) {
            loadingHint = (String) getContext().getText(R.string.listview_loading);
        }
        /*if (noMoreHint == null || noMoreHint.equals("")) {
            noMoreHint = (String) getContext().getText(R.string.nomore_loading);
        }*/
        if (loadingDoneHint == null || loadingDoneHint.equals("")) {
            loadingDoneHint = (String) getContext().getText(R.string.loading_done);
        }

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ScreenUtil.dp2px(context,80));
        layoutParams.setMargins((int) getResources().getDimension(R.dimen.textandiconmargin), 0, 0, 0);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;

        mText.setGravity(Gravity.CENTER);

        mText.setLayoutParams(layoutParams);
        addView(mText);
    }

    public void onLoadingMore() {
        progressCon.setVisibility(View.VISIBLE);
        mText.setText(loadingHint);
        this.setVisibility(View.VISIBLE);
    }

    public void onLoadMoreComplete() {
        mText.setText(loadingDoneHint);
        this.setVisibility(View.GONE);
    }

    public void onSetNoMore() {
        mText.setText(noMoreHint);
        progressCon.setVisibility(View.GONE);
        this.setVisibility(View.VISIBLE);
    }
}
